package com.crm.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crm.po.Menu;
import com.crm.service.MenuService;
import com.crm.util.RequestEx;
import com.crm.util.ResponseUtils;

public class MenuAction extends Servlet {

	private static final long serialVersionUID = 1116994082953057010L;

	public void getTree(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String menutype = RequestEx.getStringTrim(request.getParameter("menutype"));
		MenuService service = new MenuService();
		 List<Menu> list = service.getTreeByType(menutype);		
		ResponseUtils.printJson(response, null, list, null,null);
	}
	
	public void getMenuList(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String menutype = RequestEx.getStringTrim(request.getParameter("menutype"));
		MenuService service = new MenuService();
		 List<Menu> list = service.getTreeByType(menutype);		
		ResponseUtils.printJson(response, null, list, null,null);
	}
	
	public void saveOrUpdateMenu(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		MenuService service = new MenuService();

		boolean result = service.saveOrUpdateMenu(request);
		
		String value = result ? "ok": "新增数据失败，请检查您的输入数据是否有效。";
		ResponseUtils.printString(response, value);
	}
	
	public void deleteMenu(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		MenuService service = new MenuService();

		int result = service.deleteMenu(request);
		
		String value = "";
		if(result == -1){
			value = "删除数据失败，您所删除的数据分类包含有数据。";
		}else if(result == -2){
			value = "删除数据失败，请检查您的系统是否出现错误。";
		}else{
			value = "ok";
		}
		ResponseUtils.printString(response, value);
	}

}
